class CategoriesController < ApplicationController
  layout :choose_layout
  # GET /categories
  # GET /categories.xml
  def index
#   @categories = Category.find_by_sql(" SELECT a.id, a.name as AdminName, a.lastname, a.login, a.pass, a.ci FROM administrators a, categories c WHERE a.id=c.administrator_id ")
@categories = Category.find_by_sql(" SELECT c.id, c.name as CategoryName, c.category_type, a.name as AdminName  FROM administrators a, categories c WHERE a.id=c.administrator_id ")


    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @categories }
    end
  end

  # GET /categories/1
  # GET /categories/1.xml
  def show
    @category = Category.find(params[:id])
    @administrator = Administrator.find(@category.administrator_id)
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @category }
    end
  end

  # GET /categories/new
  # GET /categories/new.xml
  def new
     
    @category = Category.new
    

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @category }
    end
  end

  # GET /categories/1/edit
  def edit
    @category = Category.find(params[:id])
  end

  # POST /categories
  # POST /categories.xml
  def create
    
    @category = Category.new(params[:category])
    @category.administrator_id=session[:id]
    respond_to do |format|
      if @category.save
        flash[:notice] = t('flash.createCateg')
        logger.info '//////LA CATEGORIA SE  HA CREADO EXITOSAMENTE////// '+Time.now.to_s
        format.html { redirect_to(@category) }
        format.xml  { render :xml => @category, :status => :created, :location => @category }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /categories/1
  # PUT /categories/1.xml
  def update
    @category = Category.find(params[:id])

    respond_to do |format|
      if @category.update_attributes(params[:category])
        flash[:notice] = t('flash.updateCateg')
        logger.info '//////LA CATEGORIA  SE HA ACTUALIZADO EXITOSAMENTE////// '+Time.now.to_s
        format.html { redirect_to(@category) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
        logger.info '//////NO SE PUDO ACTUALIZAR LA CATEGORIA////// '+Time.now.to_s
      end
    end
  end
  def search_categories

   @category = Category.first(:conditions => [" name = ? ", params[:search]])
   @events= Event.find_by_sql("SELECT e.id,e.name as eventName, e.winner, e.date_ini, e.date_fin, e.positions, e.maxamount FROM events e WHERE e.category_id="+@category.id.to_s)

   if @events.size>0
   render 'categories/search_categories'
   else
     flash[:notice] = t('flash.noevent')+@category.name
     logger.info '//////NO SE ENCONTRARON EVENTOS ASOCIADOS A DICHA CATEGORIA////// '+Time.now.to_s
     redirect_to :back
   end
  end

  # DELETE /categories/1
  # DELETE /categories/1.xml
  def destroy
    @category = Category.find(params[:id])
    @category.destroy

    respond_to do |format|
      format.html { redirect_to(categories_url) }
      format.xml  { head :ok }
    end
  end


    private

  def choose_layout
    case action_name
    when "search_categories"
      "template"
    else
      "templateAdmin"
    end
  end


end
